// 使用 Promise 封装 wx.request
import {
  API_BASE_URL
} from '../config/index'
export const http = (url, data = {}, method = 'get') => {
  url = API_BASE_URL + url
  return new Promise((reslove, reject) => {
	  uni.showLoading({
	  	title: '加载中...'
	  })
      wx.request({
          url,
          data,
          method, 
          header: {
               'content-type' : 'application/json',
              Authorization: uni.getStorageSync("token")
          },
          success(res) {
              if (res.data.errCode == '10000') {
                  reslove(res.data.data)
              }else{
                  wx.showToast({
                    title: res.data.msg,
                  })
                  return Promise.reject(res.data.errMsg)
              }
          },
          fail(error) {
              reject(error)
          },
          complete() {
              wx.hideLoading()
          }
      })
  })
}

export const get = (url, data = {}) => {
  return http(url, data, 'get')
}

export const post = (url, data = {}) => {
  return http(url, data, 'post')
}